package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.os.Handler;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.viewholder.albums.MoreAlbumsCottonViewHolder;
import com.taptech.doufu.bean.AlbumsBean;

import java.util.List;

/**
 * Created by lsy on 2015/7/1.
 */
public class MoreAlbumsCottonViewAdapter<T extends MoreAlbumsCottonViewHolder> extends RecyclerView.Adapter<MoreAlbumsCottonViewHolder>
{
    private final int HEADVIEW_TYPE = 1;
    private final int FOOTVIEW_TYPE = 2;
    private final int NORMALVIEW_TYPE = 0;


    private boolean isHeadViewVisible;
    private int headViewLayoutId;
    private View headerView = null;
    private int footViewLayoutId;
    private View footerView = null;

    private Context mContext;
    private Handler mHandler;
    private T mViewHolder;

    public List<AlbumsBean> getmDataList()
    {
        return mDataList;
    }

    private List<AlbumsBean> mDataList;

    public MoreAlbumsCottonViewAdapter(Context context, List<AlbumsBean> dataList)
    {
        this.mContext = context;
        this.mDataList = dataList;
    }

    public MoreAlbumsCottonViewAdapter(Context context, List<AlbumsBean> dataList, Handler handler)
    {
        this.mContext = context;
        this.mDataList = dataList;
        this.mHandler = handler;
    }

    public MoreAlbumsCottonViewAdapter(Context context, List<AlbumsBean> dataList, Handler handler, T viewHolder)
    {
        this.mContext = context;
        this.mDataList = dataList;
        this.mHandler = handler;
        this.mViewHolder = viewHolder;
    }


    public int getItemViewType(int position)
    {
        if (mDataList != null)
        {
            if (position == 0 && headViewLayoutId != 0 && isHeadViewVisible)
            {
                return HEADVIEW_TYPE;
            } else if (position == mDataList.size() && footViewLayoutId != 0)
            {
                return FOOTVIEW_TYPE;
            } else
            {
                return NORMALVIEW_TYPE;
            }
        } else
        {
            return 0;
        }
    }

    @Override
    public MoreAlbumsCottonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = null;

        if (i == HEADVIEW_TYPE)
        {
            headerView = LayoutInflater.from(mContext).inflate(headViewLayoutId, viewGroup, false);
            view = headerView;
        } else if (i == FOOTVIEW_TYPE)
        {
            headerView = LayoutInflater.from(mContext).inflate(footViewLayoutId, viewGroup, false);
            view = footerView;
        } else
        {

            view = LayoutInflater.from(mContext).inflate(R.layout.more_albmus_item, viewGroup, false);
        }
        return new MoreAlbumsCottonViewHolder(view, mContext);
    }

    @Override
    public void onBindViewHolder(MoreAlbumsCottonViewHolder myViewHolder, int i)
    {
        try
        {
            myViewHolder.setChildContent(mDataList.get(i), i);
            myViewHolder.setHandler(mHandler);
        } catch (Exception e)
        {
            //            Log.d("tag" , "load view position ============"+mDataList.get(i).getUser().getName());
        }


    }

    @Override
    public int getItemCount()
    {
        if (mDataList != null)
        {
            return mDataList.size();
        }
        return 0;
    }

    /**
     * ===============================设置头部=======================================
     */
    public void setHeadViewVisible(int isVisible)
    {
        if (isVisible == View.GONE && mDataList != null && mDataList.size() > 0)
        {
            mDataList.remove(0);
            notifyItemRemoved(0);
            isHeadViewVisible = false;
        } else if (isVisible == View.VISIBLE)
        {
            if (mDataList != null)
            {
                mDataList.add(0, new AlbumsBean());
                isHeadViewVisible = true;
                notifyItemInserted(0);
            }
        }
    }

    /**
     * =============================getter & setter====================================
     */
    public boolean isHeadViewVisible()
    {
        return isHeadViewVisible;
    }

    public void setHeaderView(int layoutId)
    {
        /*if (mDataList == null) {
            mDataList = new ArrayList<>();
        }
        mDataList.add(0, new TestBean());*/
        this.headViewLayoutId = layoutId;
    }

    public void setFooterView(int layoutId)
    {
        this.footViewLayoutId = layoutId;
    }
}

